{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "import sys\n",
    "import warnings\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import sys\n",
    "import seaborn as sns\n",
    "import warnings\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "\n",
    "from sklearn.metrics import mean_squared_error as MSE\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.metrics import mean_absolute_error #平方绝对误差\n",
    "from sklearn.metrics import r2_score#R square\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签\n",
    "plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "feature_names = pd.read_excel(\"主要特征变量.xlsx\", na_values=np.nan)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "feature_names_list = list(feature_names['主要特征变量'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['S-ZORB.CAL_H2.PV',\n",
       " 'S-ZORB.FC_2801.PV',\n",
       " 'S-ZORB.FT_5101.PV',\n",
       " 'S-ZORB.FT_9001.PV',\n",
       " 'S-ZORB.LC_5001.PV',\n",
       " 'S-ZORB.LC_5101.PV',\n",
       " 'S-ZORB.PC_2105.PV',\n",
       " 'S-ZORB.PC_5101.PV',\n",
       " 'S-ZORB.PDT_2104.PV',\n",
       " 'S-ZORB.PT_2101.PV',\n",
       " 'S-ZORB.PT_2801.PV',\n",
       " 'S-ZORB.TC_5005.PV',\n",
       " 'S-ZORB.TE_2005.PV',\n",
       " 'S-ZORB.TE_2103.PV',\n",
       " 'S-ZORB.TE_2301.PV',\n",
       " 'S-ZORB.TE_5102.PV',\n",
       " 'S-ZORB.TE_5202.PV',\n",
       " 'S-ZORB.TE_9001.PV',\n",
       " '待生吸附剂性质：S',\n",
       " '待生吸附剂性质：焦炭',\n",
       " '原料性质：饱和烃',\n",
       " '原料性质：硫含量',\n",
       " '原料性质：密度',\n",
       " '原料性质：烯烃',\n",
       " '原料性质：辛烷值',\n",
       " '原料性质：溴值',\n",
       " '再生吸附剂性质：S',\n",
       " '再生吸附剂性质：焦炭']"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feature_names_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_325 = pd.read_excel(\"3-数据清洗后325样本数据.xlsx\", na_values=np.nan)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['样本编号', '时间', 'RON损失', '原料性质：硫含量', '原料性质：辛烷值', '原料性质：饱和烃', '原料性质：烯烃',\n",
       "       '原料性质：芳烃', '原料性质：溴值', '原料性质：密度', '产品性质：硫含量', '产品性质：辛烷值', '待生吸附剂性质：焦炭',\n",
       "       '待生吸附剂性质：S', '再生吸附剂性质：焦炭', '再生吸附剂性质：S', 'S-ZORB.CAL_H2.PV',\n",
       "       'S-ZORB.PDI_2102.PV', 'S-ZORB.PT_2801.PV', 'S-ZORB.FC_2801.PV',\n",
       "       'S-ZORB.TE_2103.PV', 'S-ZORB.TE_2005.PV', 'S-ZORB.PT_2101.PV',\n",
       "       'S-ZORB.PDT_2104.PV', 'S-ZORB.TE_2301.PV', 'S-ZORB.PT_2301.PV',\n",
       "       'S-ZORB.PC_2105.PV', 'S-ZORB.PC_5101.PV', 'S-ZORB.TC_5005.PV',\n",
       "       'S-ZORB.LC_5001.PV'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_325.columns[:30]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "feature_names_list.append('产品性质：辛烷值')\n",
    "feature_names_list.append('RON损失')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_325 = data_325[ feature_names_list ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(325, 30)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_325.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>S-ZORB.CAL_H2.PV</th>\n",
       "      <th>S-ZORB.FC_2801.PV</th>\n",
       "      <th>S-ZORB.FT_5101.PV</th>\n",
       "      <th>S-ZORB.FT_9001.PV</th>\n",
       "      <th>S-ZORB.LC_5001.PV</th>\n",
       "      <th>S-ZORB.LC_5101.PV</th>\n",
       "      <th>S-ZORB.PC_2105.PV</th>\n",
       "      <th>S-ZORB.PC_5101.PV</th>\n",
       "      <th>S-ZORB.PDT_2104.PV</th>\n",
       "      <th>S-ZORB.PT_2101.PV</th>\n",
       "      <th>...</th>\n",
       "      <th>原料性质：饱和烃</th>\n",
       "      <th>原料性质：硫含量</th>\n",
       "      <th>原料性质：密度</th>\n",
       "      <th>原料性质：烯烃</th>\n",
       "      <th>原料性质：辛烷值</th>\n",
       "      <th>原料性质：溴值</th>\n",
       "      <th>再生吸附剂性质：S</th>\n",
       "      <th>再生吸附剂性质：焦炭</th>\n",
       "      <th>产品性质：辛烷值</th>\n",
       "      <th>RON损失</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.276318</td>\n",
       "      <td>647.752380</td>\n",
       "      <td>771.817045</td>\n",
       "      <td>479.093870</td>\n",
       "      <td>55.011075</td>\n",
       "      <td>49.916519</td>\n",
       "      <td>4.980083</td>\n",
       "      <td>0.652138</td>\n",
       "      <td>101.290580</td>\n",
       "      <td>2.393991</td>\n",
       "      <td>...</td>\n",
       "      <td>53.230000</td>\n",
       "      <td>188.0</td>\n",
       "      <td>726.085714</td>\n",
       "      <td>24.400000</td>\n",
       "      <td>90.6</td>\n",
       "      <td>61.487143</td>\n",
       "      <td>5.98</td>\n",
       "      <td>1.84</td>\n",
       "      <td>89.22</td>\n",
       "      <td>1.38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.277127</td>\n",
       "      <td>651.821920</td>\n",
       "      <td>678.940915</td>\n",
       "      <td>470.983570</td>\n",
       "      <td>54.996157</td>\n",
       "      <td>49.955209</td>\n",
       "      <td>4.979703</td>\n",
       "      <td>0.652110</td>\n",
       "      <td>101.544925</td>\n",
       "      <td>2.394113</td>\n",
       "      <td>...</td>\n",
       "      <td>52.300000</td>\n",
       "      <td>169.0</td>\n",
       "      <td>731.300000</td>\n",
       "      <td>26.400000</td>\n",
       "      <td>90.5</td>\n",
       "      <td>61.880000</td>\n",
       "      <td>4.38</td>\n",
       "      <td>0.55</td>\n",
       "      <td>89.32</td>\n",
       "      <td>1.18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.277013</td>\n",
       "      <td>650.747150</td>\n",
       "      <td>563.586215</td>\n",
       "      <td>433.017815</td>\n",
       "      <td>55.021892</td>\n",
       "      <td>50.215176</td>\n",
       "      <td>4.955568</td>\n",
       "      <td>0.652395</td>\n",
       "      <td>101.618320</td>\n",
       "      <td>2.394545</td>\n",
       "      <td>...</td>\n",
       "      <td>52.300000</td>\n",
       "      <td>177.0</td>\n",
       "      <td>729.614286</td>\n",
       "      <td>26.314286</td>\n",
       "      <td>90.7</td>\n",
       "      <td>61.722857</td>\n",
       "      <td>5.82</td>\n",
       "      <td>1.89</td>\n",
       "      <td>89.32</td>\n",
       "      <td>1.38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.277222</td>\n",
       "      <td>652.669055</td>\n",
       "      <td>681.970820</td>\n",
       "      <td>440.395785</td>\n",
       "      <td>55.041748</td>\n",
       "      <td>49.877925</td>\n",
       "      <td>4.990756</td>\n",
       "      <td>0.652089</td>\n",
       "      <td>105.029760</td>\n",
       "      <td>2.392854</td>\n",
       "      <td>...</td>\n",
       "      <td>52.300000</td>\n",
       "      <td>159.0</td>\n",
       "      <td>725.400000</td>\n",
       "      <td>26.100000</td>\n",
       "      <td>90.4</td>\n",
       "      <td>61.330000</td>\n",
       "      <td>4.67</td>\n",
       "      <td>0.98</td>\n",
       "      <td>89.02</td>\n",
       "      <td>1.38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.277310</td>\n",
       "      <td>649.340920</td>\n",
       "      <td>715.346780</td>\n",
       "      <td>411.576765</td>\n",
       "      <td>55.026846</td>\n",
       "      <td>49.900563</td>\n",
       "      <td>4.973214</td>\n",
       "      <td>0.652213</td>\n",
       "      <td>105.937455</td>\n",
       "      <td>2.392715</td>\n",
       "      <td>...</td>\n",
       "      <td>52.242857</td>\n",
       "      <td>173.0</td>\n",
       "      <td>725.428571</td>\n",
       "      <td>26.671429</td>\n",
       "      <td>89.6</td>\n",
       "      <td>61.332857</td>\n",
       "      <td>4.52</td>\n",
       "      <td>0.83</td>\n",
       "      <td>88.32</td>\n",
       "      <td>1.28</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 30 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   S-ZORB.CAL_H2.PV  S-ZORB.FC_2801.PV  S-ZORB.FT_5101.PV  S-ZORB.FT_9001.PV  \\\n",
       "0          0.276318         647.752380         771.817045         479.093870   \n",
       "1          0.277127         651.821920         678.940915         470.983570   \n",
       "2          0.277013         650.747150         563.586215         433.017815   \n",
       "3          0.277222         652.669055         681.970820         440.395785   \n",
       "4          0.277310         649.340920         715.346780         411.576765   \n",
       "\n",
       "   S-ZORB.LC_5001.PV  S-ZORB.LC_5101.PV  S-ZORB.PC_2105.PV  S-ZORB.PC_5101.PV  \\\n",
       "0          55.011075          49.916519           4.980083           0.652138   \n",
       "1          54.996157          49.955209           4.979703           0.652110   \n",
       "2          55.021892          50.215176           4.955568           0.652395   \n",
       "3          55.041748          49.877925           4.990756           0.652089   \n",
       "4          55.026846          49.900563           4.973214           0.652213   \n",
       "\n",
       "   S-ZORB.PDT_2104.PV  S-ZORB.PT_2101.PV  ...   原料性质：饱和烃  原料性质：硫含量  \\\n",
       "0          101.290580           2.393991  ...  53.230000     188.0   \n",
       "1          101.544925           2.394113  ...  52.300000     169.0   \n",
       "2          101.618320           2.394545  ...  52.300000     177.0   \n",
       "3          105.029760           2.392854  ...  52.300000     159.0   \n",
       "4          105.937455           2.392715  ...  52.242857     173.0   \n",
       "\n",
       "      原料性质：密度    原料性质：烯烃  原料性质：辛烷值    原料性质：溴值  再生吸附剂性质：S  再生吸附剂性质：焦炭  \\\n",
       "0  726.085714  24.400000      90.6  61.487143       5.98        1.84   \n",
       "1  731.300000  26.400000      90.5  61.880000       4.38        0.55   \n",
       "2  729.614286  26.314286      90.7  61.722857       5.82        1.89   \n",
       "3  725.400000  26.100000      90.4  61.330000       4.67        0.98   \n",
       "4  725.428571  26.671429      89.6  61.332857       4.52        0.83   \n",
       "\n",
       "   产品性质：辛烷值  RON损失  \n",
       "0     89.22   1.38  \n",
       "1     89.32   1.18  \n",
       "2     89.32   1.38  \n",
       "3     89.02   1.38  \n",
       "4     88.32   1.28  \n",
       "\n",
       "[5 rows x 30 columns]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_325.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = data_325[ data_325.columns[:28] ]\n",
    "y_ron = data_325[ data_325.columns[28] ]\n",
    "y_ron_loss = data_325[ data_325.columns[29] ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>S-ZORB.CAL_H2.PV</th>\n",
       "      <th>S-ZORB.FC_2801.PV</th>\n",
       "      <th>S-ZORB.FT_5101.PV</th>\n",
       "      <th>S-ZORB.FT_9001.PV</th>\n",
       "      <th>S-ZORB.LC_5001.PV</th>\n",
       "      <th>S-ZORB.LC_5101.PV</th>\n",
       "      <th>S-ZORB.PC_2105.PV</th>\n",
       "      <th>S-ZORB.PC_5101.PV</th>\n",
       "      <th>S-ZORB.PDT_2104.PV</th>\n",
       "      <th>S-ZORB.PT_2101.PV</th>\n",
       "      <th>...</th>\n",
       "      <th>待生吸附剂性质：S</th>\n",
       "      <th>待生吸附剂性质：焦炭</th>\n",
       "      <th>原料性质：饱和烃</th>\n",
       "      <th>原料性质：硫含量</th>\n",
       "      <th>原料性质：密度</th>\n",
       "      <th>原料性质：烯烃</th>\n",
       "      <th>原料性质：辛烷值</th>\n",
       "      <th>原料性质：溴值</th>\n",
       "      <th>再生吸附剂性质：S</th>\n",
       "      <th>再生吸附剂性质：焦炭</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.276318</td>\n",
       "      <td>647.752380</td>\n",
       "      <td>771.817045</td>\n",
       "      <td>479.093870</td>\n",
       "      <td>55.011075</td>\n",
       "      <td>49.916519</td>\n",
       "      <td>4.980083</td>\n",
       "      <td>0.652138</td>\n",
       "      <td>101.290580</td>\n",
       "      <td>2.393991</td>\n",
       "      <td>...</td>\n",
       "      <td>7.30</td>\n",
       "      <td>2.32</td>\n",
       "      <td>53.230000</td>\n",
       "      <td>188.0</td>\n",
       "      <td>726.085714</td>\n",
       "      <td>24.400000</td>\n",
       "      <td>90.6</td>\n",
       "      <td>61.487143</td>\n",
       "      <td>5.98</td>\n",
       "      <td>1.84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.277127</td>\n",
       "      <td>651.821920</td>\n",
       "      <td>678.940915</td>\n",
       "      <td>470.983570</td>\n",
       "      <td>54.996157</td>\n",
       "      <td>49.955209</td>\n",
       "      <td>4.979703</td>\n",
       "      <td>0.652110</td>\n",
       "      <td>101.544925</td>\n",
       "      <td>2.394113</td>\n",
       "      <td>...</td>\n",
       "      <td>7.34</td>\n",
       "      <td>2.37</td>\n",
       "      <td>52.300000</td>\n",
       "      <td>169.0</td>\n",
       "      <td>731.300000</td>\n",
       "      <td>26.400000</td>\n",
       "      <td>90.5</td>\n",
       "      <td>61.880000</td>\n",
       "      <td>4.38</td>\n",
       "      <td>0.55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.277013</td>\n",
       "      <td>650.747150</td>\n",
       "      <td>563.586215</td>\n",
       "      <td>433.017815</td>\n",
       "      <td>55.021892</td>\n",
       "      <td>50.215176</td>\n",
       "      <td>4.955568</td>\n",
       "      <td>0.652395</td>\n",
       "      <td>101.618320</td>\n",
       "      <td>2.394545</td>\n",
       "      <td>...</td>\n",
       "      <td>7.27</td>\n",
       "      <td>2.43</td>\n",
       "      <td>52.300000</td>\n",
       "      <td>177.0</td>\n",
       "      <td>729.614286</td>\n",
       "      <td>26.314286</td>\n",
       "      <td>90.7</td>\n",
       "      <td>61.722857</td>\n",
       "      <td>5.82</td>\n",
       "      <td>1.89</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.277222</td>\n",
       "      <td>652.669055</td>\n",
       "      <td>681.970820</td>\n",
       "      <td>440.395785</td>\n",
       "      <td>55.041748</td>\n",
       "      <td>49.877925</td>\n",
       "      <td>4.990756</td>\n",
       "      <td>0.652089</td>\n",
       "      <td>105.029760</td>\n",
       "      <td>2.392854</td>\n",
       "      <td>...</td>\n",
       "      <td>7.35</td>\n",
       "      <td>3.08</td>\n",
       "      <td>52.300000</td>\n",
       "      <td>159.0</td>\n",
       "      <td>725.400000</td>\n",
       "      <td>26.100000</td>\n",
       "      <td>90.4</td>\n",
       "      <td>61.330000</td>\n",
       "      <td>4.67</td>\n",
       "      <td>0.98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.277310</td>\n",
       "      <td>649.340920</td>\n",
       "      <td>715.346780</td>\n",
       "      <td>411.576765</td>\n",
       "      <td>55.026846</td>\n",
       "      <td>49.900563</td>\n",
       "      <td>4.973214</td>\n",
       "      <td>0.652213</td>\n",
       "      <td>105.937455</td>\n",
       "      <td>2.392715</td>\n",
       "      <td>...</td>\n",
       "      <td>6.58</td>\n",
       "      <td>2.45</td>\n",
       "      <td>52.242857</td>\n",
       "      <td>173.0</td>\n",
       "      <td>725.428571</td>\n",
       "      <td>26.671429</td>\n",
       "      <td>89.6</td>\n",
       "      <td>61.332857</td>\n",
       "      <td>4.52</td>\n",
       "      <td>0.83</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 28 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   S-ZORB.CAL_H2.PV  S-ZORB.FC_2801.PV  S-ZORB.FT_5101.PV  S-ZORB.FT_9001.PV  \\\n",
       "0          0.276318         647.752380         771.817045         479.093870   \n",
       "1          0.277127         651.821920         678.940915         470.983570   \n",
       "2          0.277013         650.747150         563.586215         433.017815   \n",
       "3          0.277222         652.669055         681.970820         440.395785   \n",
       "4          0.277310         649.340920         715.346780         411.576765   \n",
       "\n",
       "   S-ZORB.LC_5001.PV  S-ZORB.LC_5101.PV  S-ZORB.PC_2105.PV  S-ZORB.PC_5101.PV  \\\n",
       "0          55.011075          49.916519           4.980083           0.652138   \n",
       "1          54.996157          49.955209           4.979703           0.652110   \n",
       "2          55.021892          50.215176           4.955568           0.652395   \n",
       "3          55.041748          49.877925           4.990756           0.652089   \n",
       "4          55.026846          49.900563           4.973214           0.652213   \n",
       "\n",
       "   S-ZORB.PDT_2104.PV  S-ZORB.PT_2101.PV  ...  待生吸附剂性质：S  待生吸附剂性质：焦炭  \\\n",
       "0          101.290580           2.393991  ...       7.30        2.32   \n",
       "1          101.544925           2.394113  ...       7.34        2.37   \n",
       "2          101.618320           2.394545  ...       7.27        2.43   \n",
       "3          105.029760           2.392854  ...       7.35        3.08   \n",
       "4          105.937455           2.392715  ...       6.58        2.45   \n",
       "\n",
       "    原料性质：饱和烃  原料性质：硫含量     原料性质：密度    原料性质：烯烃  原料性质：辛烷值    原料性质：溴值  再生吸附剂性质：S  \\\n",
       "0  53.230000     188.0  726.085714  24.400000      90.6  61.487143       5.98   \n",
       "1  52.300000     169.0  731.300000  26.400000      90.5  61.880000       4.38   \n",
       "2  52.300000     177.0  729.614286  26.314286      90.7  61.722857       5.82   \n",
       "3  52.300000     159.0  725.400000  26.100000      90.4  61.330000       4.67   \n",
       "4  52.242857     173.0  725.428571  26.671429      89.6  61.332857       4.52   \n",
       "\n",
       "   再生吸附剂性质：焦炭  \n",
       "0        1.84  \n",
       "1        0.55  \n",
       "2        1.89  \n",
       "3        0.98  \n",
       "4        0.83  \n",
       "\n",
       "[5 rows x 28 columns]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_train_ron, x_test_ron, y_train_ron, y_test_ron = train_test_split(X, y_ron, test_size=0.3, random_state=1)\n",
    "x_train_ron_loss, x_test_ron_loss, y_train_ron_loss, y_test_ron_loss = train_test_split(X, y_ron_loss, test_size=0.3, random_state=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.neighbors import KNeighborsRegressor\n",
    "from sklearn.svm import SVR\n",
    "from sklearn.linear_model import Lasso\n",
    "from sklearn.linear_model import Ridge\n",
    "from sklearn.neural_network import MLPRegressor\n",
    "from sklearn.tree import DecisionTreeRegressor\n",
    "from sklearn.tree import ExtraTreeRegressor\n",
    "from xgboost import XGBRegressor\n",
    "from sklearn.ensemble import RandomForestRegressor\n",
    "from sklearn.ensemble import AdaBoostRegressor\n",
    "from sklearn.ensemble import GradientBoostingRegressor\n",
    "from sklearn.ensemble import BaggingRegressor"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ron model 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "随机森林回归模型\n",
      "MSE_SCORE =  0.05831773209724128\n",
      "RMSE_SCORE =  0.2414906459829061\n",
      "R_Square =  0.9406813823471101\n"
     ]
    }
   ],
   "source": [
    "model_RFR = RandomForestRegressor(max_depth=5, n_estimators=100, random_state=666)\n",
    "model_RFR.fit(x_train_ron, y_train_ron)\n",
    "y_ron_predict_1 = model_RFR.predict(x_test_ron)\n",
    "model_fearture = model_RFR.feature_importances_\n",
    "mse_score = MSE(y_test_ron, y_ron_predict_1)\n",
    "rmse_score = np.sqrt(mse_score)\n",
    "mae_score = mean_absolute_error(y_test_ron, y_ron_predict_1)\n",
    "rr_score = r2_score(y_test_ron, y_ron_predict_1)\n",
    "print(\"随机森林回归模型\")\n",
    "print(\"MSE_SCORE = \", mse_score)\n",
    "print(\"RMSE_SCORE = \", rmse_score)\n",
    "print(\"R_Square = \", rr_score)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ron model 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "极端梯度提升回归模型\n",
      "MSE_SCORE =  0.3061509637548889\n",
      "RMSE_SCORE =  0.5533091032640697\n",
      "R_Square =  0.6885946810695835\n"
     ]
    }
   ],
   "source": [
    "model_XGBR = XGBRegressor(learning_rate=0.1, max_depth=5, min_child_weight=6, n_estimators=50, random_state=666)\n",
    "model_XGBR.fit(x_train_ron, y_train_ron)\n",
    "y_ron_predict_2 = model_XGBR.predict(x_test_ron)\n",
    "model_fearture = model_XGBR.feature_importances_\n",
    "mse_score = MSE(y_test_ron, y_ron_predict_2)\n",
    "rmse_score = np.sqrt(mse_score)\n",
    "mae_score = mean_absolute_error(y_test_ron, y_ron_predict_2)\n",
    "rr_score = r2_score(y_test_ron, y_ron_predict_2)\n",
    "print(\"极端梯度提升回归模型\")\n",
    "print(\"MSE_SCORE = \", mse_score)\n",
    "print(\"RMSE_SCORE = \", rmse_score)\n",
    "print(\"R_Square = \", rr_score)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ron model 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AdaBoost回归模型\n",
      "MSE_SCORE =  0.06320141472866121\n",
      "RMSE_SCORE =  0.2513989155280134\n",
      "R_Square =  0.9357138828862563\n"
     ]
    }
   ],
   "source": [
    "model_AdaBR = AdaBoostRegressor(learning_rate=0.1, n_estimators=50, random_state=666)\n",
    "model_AdaBR.fit(x_train_ron, y_train_ron)\n",
    "y_ron_predict_3 = model_AdaBR.predict(x_test_ron)\n",
    "model_fearture = model_AdaBR.feature_importances_\n",
    "mse_score = MSE(y_test_ron, y_ron_predict_3)\n",
    "rmse_score = np.sqrt(mse_score)\n",
    "mae_score = mean_absolute_error(y_test_ron, y_ron_predict_3)\n",
    "rr_score = r2_score(y_test_ron, y_ron_predict_3)\n",
    "print(\"AdaBoost回归模型\")\n",
    "print(\"MSE_SCORE = \", mse_score)\n",
    "print(\"RMSE_SCORE = \", rmse_score)\n",
    "print(\"R_Square = \", rr_score)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ron model blending"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_blending = (y_ron_predict_1 + y_ron_predict_3 + y_ron_predict_3)/3.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "三种模型融合\n",
      "MSE_SCORE =  0.059575923317088955\n",
      "RMSE_SCORE =  0.24408179636566296\n",
      "R_Square =  0.9394015972591043\n"
     ]
    }
   ],
   "source": [
    "mse_score = MSE(y_test_ron, y_blending)\n",
    "rmse_score = np.sqrt(mse_score)\n",
    "mae_score = mean_absolute_error(y_test_ron, y_blending)\n",
    "rr_score = r2_score(y_test_ron, y_blending)\n",
    "print(\"三种模型融合\")\n",
    "print(\"MSE_SCORE = \", mse_score)\n",
    "print(\"RMSE_SCORE = \", rmse_score)\n",
    "print(\"R_Square = \", rr_score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "x_test_ron_df = pd.DataFrame(x_test_ron['原料性质：辛烷值'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "x_test_ron_df = x_test_ron_df.reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_pre_ron = pd.DataFrame(y_blending)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "y_pre_ron.columns = ['产品性质：辛烷值']\n",
    "y_pre_ron = y_pre_ron.reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_ron_blending = x_test_ron_df[\"原料性质：辛烷值\"] - y_pre_ron['产品性质：辛烷值']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ron_loss model blending"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "三种模型融合\n",
      "MSE_SCORE =  0.059575923317088955\n",
      "RMSE_SCORE =  0.24408179636566296\n"
     ]
    }
   ],
   "source": [
    "mse_score = MSE(y_test_ron_loss, y_ron_blending)\n",
    "rmse_score = np.sqrt(mse_score)\n",
    "mae_score = mean_absolute_error(y_test_ron_loss, y_ron_blending)\n",
    "rr_score = r2_score(y_test_ron_loss, y_ron_blending)\n",
    "print(\"三种模型融合\")\n",
    "print(\"MSE_SCORE = \", mse_score)\n",
    "print(\"RMSE_SCORE = \", rmse_score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ron_loss model 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "随机森林回归模型\n",
      "MSE_SCORE =  0.037328547730127795\n",
      "RMSE_SCORE =  0.19320597229414985\n",
      "R_Square =  0.14599618334122277\n"
     ]
    }
   ],
   "source": [
    "model_RFR = RandomForestRegressor(max_depth=5, n_estimators=100, random_state=666)\n",
    "model_RFR.fit(x_train_ron_loss, y_train_ron_loss)\n",
    "y_ron_loss_predict_1 = model_RFR.predict(x_test_ron_loss)\n",
    "model_fearture = model_RFR.feature_importances_\n",
    "mse_score = MSE(y_test_ron_loss, y_ron_loss_predict_1)\n",
    "rmse_score = np.sqrt(mse_score)\n",
    "mae_score = mean_absolute_error(y_test_ron_loss, y_ron_loss_predict_1)\n",
    "rr_score = r2_score(y_test_ron_loss, y_ron_loss_predict_1)\n",
    "print(\"随机森林回归模型\")\n",
    "print(\"MSE_SCORE = \", mse_score)\n",
    "print(\"RMSE_SCORE = \", rmse_score)\n",
    "print(\"R_Square = \", rr_score)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ron_loss model 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "极端梯度提升回归模型\n",
      "MSE_SCORE =  0.03904035855916872\n",
      "RMSE_SCORE =  0.19758633191384653\n",
      "R_Square =  0.10683331550163389\n"
     ]
    }
   ],
   "source": [
    "model_XGBR = XGBRegressor(learning_rate=0.1, max_depth=5, min_child_weight=6, n_estimators=50, random_state=666)\n",
    "model_XGBR.fit(x_train_ron_loss, y_train_ron_loss)\n",
    "y_ron_loss_predict_2 = model_XGBR.predict(x_test_ron_loss)\n",
    "model_fearture = model_XGBR.feature_importances_\n",
    "mse_score = MSE(y_test_ron_loss, y_ron_loss_predict_2)\n",
    "rmse_score = np.sqrt(mse_score)\n",
    "mae_score = mean_absolute_error(y_test_ron_loss, y_ron_loss_predict_2)\n",
    "rr_score = r2_score(y_test_ron_loss, y_ron_loss_predict_2)\n",
    "print(\"极端梯度提升回归模型\")\n",
    "print(\"MSE_SCORE = \", mse_score)\n",
    "print(\"RMSE_SCORE = \", rmse_score)\n",
    "print(\"R_Square = \", rr_score)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ron_loss model 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AdaBoost回归模型\n",
      "MSE_SCORE =  0.040146344438551015\n",
      "RMSE_SCORE =  0.20036552707127794\n",
      "R_Square =  0.08153053198614035\n"
     ]
    }
   ],
   "source": [
    "model_AdaBR = AdaBoostRegressor(learning_rate=0.1, n_estimators=50, random_state=666)\n",
    "model_AdaBR.fit(x_train_ron_loss, y_train_ron_loss)\n",
    "y_ron_loss_predict_3 = model_AdaBR.predict(x_test_ron_loss)\n",
    "model_fearture = model_AdaBR.feature_importances_\n",
    "mse_score = MSE(y_test_ron_loss, y_ron_loss_predict_3)\n",
    "rmse_score = np.sqrt(mse_score)\n",
    "mae_score = mean_absolute_error(y_test_ron_loss, y_ron_loss_predict_3)\n",
    "rr_score = r2_score(y_test_ron_loss, y_ron_loss_predict_3)\n",
    "print(\"AdaBoost回归模型\")\n",
    "print(\"MSE_SCORE = \", mse_score)\n",
    "print(\"RMSE_SCORE = \", rmse_score)\n",
    "print(\"R_Square = \", rr_score)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ron_loss model blending"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_ron_loss_blending = (y_ron_loss_predict_1 + y_ron_loss_predict_2 + y_ron_loss_predict_3)/3.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "三种模型融合\n",
      "MSE_SCORE =  0.037526332262169226\n",
      "RMSE_SCORE =  0.19371714498765777\n"
     ]
    }
   ],
   "source": [
    "mse_score = MSE(y_test_ron_loss, y_ron_loss_blending)\n",
    "rmse_score = np.sqrt(mse_score)\n",
    "mae_score = mean_absolute_error(y_test_ron_loss, y_ron_loss_blending)\n",
    "rr_score = r2_score(y_test_ron_loss, y_ron_loss_blending)\n",
    "print(\"三种模型融合\")\n",
    "print(\"MSE_SCORE = \", mse_score)\n",
    "print(\"RMSE_SCORE = \", rmse_score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
